package com.bkitmobile.kesu.android.GUI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bkitmobile.kesu.android.ClassInfo.CmtLocation;
import com.bkitmobile.kesu.android.ClassInfo.PlaceShortInfo;
import com.bkitmobile.kesu.android.connectServer.Request;
import com.bkitmobile.kesu.android.connectServer.ServiceCallback;
import com.bkitmobile.kesu.android.connectServer.staticClass;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapFocus extends MapActivity {
	private MapView map;
	private ViewGroup zoom;
	private PlaceShortInfo pInfo;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.map);
		Bundle extras = getIntent().getExtras();
		pInfo = (PlaceShortInfo) extras.getSerializable("pInfo");
		GeoPoint point = new GeoPoint(pInfo.getLatE6(), pInfo.getLongE6());
		map=(MapView)findViewById(R.id.map);
		zoom=(ViewGroup)findViewById(R.id.zoom);
		zoom.addView(map.getZoomControls());
		map.getController().setZoom(20);
		PlaceOverlay placeOverlay = new PlaceOverlay(
				staticClass.getDrawable(pInfo.getType(), this), pInfo.getType());
		placeOverlay.addItems(point, pInfo.getName());
		map.getOverlays().add(placeOverlay);
		map.getController().setCenter(point);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	private class PlaceOverlay extends ItemizedOverlay<OverlayItem>
	{
		public PlaceOverlay(Drawable marker,String type) {
			super(marker);
			// TODO Auto-generated constructor stub
			this.type = type;

		}

		private List<OverlayItem> items=new ArrayList<OverlayItem>();
		private Drawable marker=null;
		private String type;
		@Override
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return(items.get(i));
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return(items.size());
		}
		public void addItems(GeoPoint point,String name)
		{
			if (items.contains(new OverlayItem(
					point
	        		  , type, name))) return;
			OverlayItem item = new OverlayItem(point, type, name);
			items.add(item);
			populate();
		}
		
		@Override
		protected boolean onTap(int index) {
			//TODO: get info to know "Is it a favorite Place and set text for btnLike"
			
			return super.onTap(index);
		}
	}

}
